package com.cf.back;

import com.cf.back.permission.controller.IndexController;
import com.cf.back.permission.controller.SystemPermissionController;
import com.cf.back.permission.controller.SystemRoleController;
import com.cf.back.user.controller.ThirdPartUserController;
import com.cf.back.user.controller.UserInfoController;
import com.cf.user.permission.EnablePermission;
import com.cf.user.permission.PermissionProperty;
import com.cf.user.permission.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.freemarker.FreeMarkerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * game-server-UserSysConfig
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/5/28 下午 07:20
 */
@Slf4j
@EnableConfigurationProperties(PermissionProperty.class)
public class UserAdminConfig extends WebMvcConfigurerAdapter implements InitializingBean {
    public UserAdminConfig() {
        log.info("实例化PermissionConfig");
    }

    //#####################权限########################################


    @Bean
    @ConditionalOnMissingBean(PermissionManager.class)
    public PermissionManager permissionManager() {
        log.info("未找到自定义的PermissionManager，实例化默认的DefaultPermissionManagerImpl");
        return new DefaultPermissionManagerImpl();
    }

    @Bean
    @ConditionalOnMissingBean(SystemPermissionService.class)
    public SystemPermissionService systemPermissionService() {
        log.info("未找到自定义的PermissionService，实例化默认的SystemPermissionServiceImpl");
        return new SystemPermissionServiceImpl();
    }

    @Bean
    @ConditionalOnMissingBean(SystemRoleServiceImpl.class)
    public SystemRoleServiceImpl systemRoleService() {
        log.info("未找到自定义的systemRoleService，实例化默认的SystemRoleServiceImpl");
        return new SystemRoleServiceImpl();
    }


    //########################controller#################


    @Bean
    public SystemPermissionController systemPermissionController() {
        log.info("未找到自定义的SystemPermissionController，实例化默认的SystemPermissionController");
        return new SystemPermissionController();
    }

    @Bean
    public SystemRoleController systemRoleController() {
        log.info("未找到自定义的SystemRoleController，实例化默认的SystemRoleController");
        return new SystemRoleController();
    }

    @Bean
    public IndexController indexController() {
        log.info("未找到自定义的IndexController，实例化默认的IndexController");
        return new IndexController();
    }


    @Bean
    public ThirdPartUserController thirdPartUserController() {
        log.info("未找到自定义的ThirdPartUserController，实例化默认的ThirdPartUserController");
        return new ThirdPartUserController();
    }

    @Bean
    public UserInfoController userInfoController() {
        log.info("未找到自定义的UserInfoController，实例化默认的UserInfoController");
        return new UserInfoController();
    }

    @Autowired
    private FreeMarkerProperties freeMarkerProperties;

    @Override
    public void afterPropertiesSet() throws Exception {
        String[] templateLoaderPath = freeMarkerProperties.getTemplateLoaderPath();
        ArrayList<String> paths = new ArrayList();
        paths.addAll(Arrays.asList(templateLoaderPath));
        paths.add("classpath:/template-permission/");
        paths.add("classpath:/template-user/");
        String[] strings = paths.toArray(new String[paths.size()]);
        freeMarkerProperties.setTemplateLoaderPath(strings);
    }

}
